package com.yix.admin.modules.system.repository;

import com.yix.admin.modules.system.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/1/21 9:24
 */
public interface SysUserRepository extends JpaRepository<SysUser, String>, JpaSpecificationExecutor<SysUser> {
    /**
     * 根据用户名查询
     * @param username 用户名
     * @return /
     */
    SysUser findByUsername(String username);

    /**
     * 根据邮箱查询
     * @param email 邮箱
     * @return /
     */
    SysUser findByEmail(String email);

    /**
     * 根据菜单查询
     * @param id 菜单ID
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, users_roles ur, roles_menus rm WHERE\n" +
            "u.id = ur.user_id AND ur.role_id = rm.role_id AND rm.menu_id = ?1 group by u.id", nativeQuery = true)
    List<SysUser> findByMenuId(String id);

    /**
     * 根据角色中的部门查询
     * @param id /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, users_roles r, roles_depts d WHERE " +
            "u.id = r.user_id AND r.role_id = d.role_id AND r.role_id = ?1 group by u.id", nativeQuery = true)
    List<SysUser> findByDeptRoleId(String id);

    /**
     * 根据角色查询用户
     * @param roleId /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, users_roles r WHERE" +
            " u.id = r.user_id AND r.role_id = ?1", nativeQuery = true)
    List<SysUser> findByRoleId(String roleId);

    /**
     * 根据手机号查询
     * @param phone 手机号
     * @return /
     */
    SysUser findByPhone(String phone);

    /**
     * 根据部门查询
     * @param deptIds /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u WHERE u.dept_id IN ?1", nativeQuery = true)
    int countByDepts(Set<String> deptIds);

    /**
     * 根据角色查询
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, users_roles r WHERE " +
            "u.id = r.user_id AND r.role_id in ?1", nativeQuery = true)
    int countByRoles(Set<String> ids);

    /**
     * 根据岗位查询
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, users_jobs j WHERE u.id = j.user_id AND j.job_id IN ?1", nativeQuery = true)
    int countByJobs(Set<String> ids);

    /**
     * 修改密码
     * @param username 用户名
     * @param pass 密码
     * @param lastPasswordResetTime /
     */
    @Modifying
    @Query(value = "update sys_user set password = ?2 , pwd_reset_time = ?3 where username = ?1",nativeQuery = true)
    void updatePass(String username, String pass, Date lastPasswordResetTime);

    /**
     * 修改邮箱
     * @param username 用户名
     * @param email 邮箱
     */
    @Modifying
    @Query(value = "update sys_user set email = ?2 where username = ?1",nativeQuery = true)
    void updateEmail(String username, String email);

    /**
     * 根据Id删除
     * @param ids /
     */
    void deleteAllByIdIn(Set<String> ids);
}
